#
# quickpool project
#
AC_INIT(quickpool, 1.0)
AC_CONFIG_HEADERS(config.h)
AC_CONFIG_SRCDIR([configure.ac])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_AUX_DIR([m4])

AC_PREFIX_DEFAULT([/opt/quickpool-1.0])
AC_DEFINE(_QUICKPOOL_PROJECT_, "QUICKPOOL 1.0", "Set project string in binaries.")
AC_DEFINE(QUICKPOOL_VERSION, 10, "Current QUICKPOOL version.")
#AC_DEFINE(REL_DATE, "Jan 1 2023", "Set the release date.")

# Initialize automake
AM_INIT_AUTOMAKE

# Experimental scheduling module
# Leave for future
#AM_CONDITIONAL([SCHED_EXPERIMENTAL], [false])

# Clean output if possible
m4_ifdef([AM_SILENT_RULES],[AM_SILENT_RULES([yes])])

# Check for programs
AC_PROG_CC
AC_PROG_INSTALL
AC_PROG_RANLIB
AC_PROG_YACC
AC_PROG_LEX
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_LIBTOOL

# --enable-debug to turn on -Werror and enable assert()
#if test "x$ac_compiler_gnu" = xyes; then
#    CFLAGS="$CFLAGS -Wall -ggdb -O0 -Wno-switch -fPIC -rdynamic"
#    CFLAGS="-Wall -O3 -fPIC -rdynamic"
#fi
AC_ARG_ENABLE([debug],
  [AS_HELP_STRING([--enable-debug], [enable DEBUG mode [default=no]])],
  [enable_debug=$enableval],
  [enable_debug=no])

echo enable-debug=$enable_debug
AM_CONDITIONAL([ENABLE_DEBUG], [false])
if test "x$enable_debug" == "xyes"; then
    CFLAGS="$CFLAGS -Wall -ggdb -O0 -fPIC -rdynamic"
    AM_CONDITIONAL([ENABLE_DEBUG], [true])
else
    CFLAGS="-Wall -O3 -fPIC -rdynamic"
fi

# --with-hwloc to turn on numa support
AM_CONDITIONAL([HWLOC], [false])
AC_ARG_WITH(
    [hwloc],
    [AS_HELP_STRING(
        [--with-hwloc[[=DIR]]],
        [Specify path to hwloc installation]
    )],
    [AS_IF([test "x$withval" != "xyes"], [CPPFLAGS="-I$withval/include $CPPFLAGS"
        LDFLAGS="-L$withval/lib -L$withval/lib64 $LDFLAGS"])
        AC_CHECK_HEADERS([hwloc.h], [], [AC_MSG_ERROR([cannot build quickpool without hwloc])])
        AC_CHECK_HEADER([numa.h], [], [AC_MSG_ERROR([cannot build quickpool without numa])])
        AM_CONDITIONAL([HWLOC], [true])
])

AC_CHECK_HEADERS([yaml.h],[], [
 AC_MSG_ERROR([cannot build quickpool without yaml-devel])
])

# Check for tcl-devel files, try all 8.X versions
AC_CHECK_HEADERS([tcl.h], [], [
 AC_CHECK_HEADERS([tcl/tcl.h], [], [
  AC_CHECK_HEADERS([tcl8.6/tcl.h], [], [
  AC_CHECK_HEADERS([tcl86/tcl.h], [], [
   AC_CHECK_HEADERS([tcl8.5/tcl.h], [], [
   AC_CHECK_HEADERS([tcl85/tcl.h], [], [
    AC_CHECK_HEADERS([tcl8.4/tcl.h], [], [
    AC_CHECK_HEADERS([tcl84/tcl.h], [], [
     AC_CHECK_HEADERS([tcl8.3/tcl.h], [], [
     AC_CHECK_HEADERS([tcl83/tcl.h], [], [
      AC_CHECK_HEADERS([tcl8.2/tcl.h], [], [
      AC_CHECK_HEADERS([tcl82/tcl.h], [], [
       AC_CHECK_HEADERS([tcl8.1/tcl.h], [], [
       AC_CHECK_HEADERS([tcl81/tcl.h], [], [
        AC_CHECK_HEADERS([tcl8.0/tcl.h], [], [
        AC_CHECK_HEADERS([tcl80/tcl.h], [], [
 AC_MSG_ERROR([cannot build quickpool without tcl-devel])
])])])])])])])])])])])])])])])])


AM_CONDITIONAL([ENABLE_TIRPC], [false])
AC_ARG_ENABLE([tirpc],
  [AS_HELP_STRING([--enable-tirpc], [enable tirpc [default=no]])],
  [zmq_enable_tirpc=$enableval],
  [zmq_enable_tirpc=no])
echo enable_tirpc=$zmq_enable_tirpc
if test "x$zmq_enable_tirpc" == "xyes" -o "x$zmq_enable_tirpc" == "x"; then
  AM_CONDITIONAL([ENABLE_TIRPC], [true])
  CPPFLAGS="$CPPFLAGS -I/usr/include/tirpc"
fi

AC_ARG_ENABLE([cuda],
  [AS_HELP_STRING([--enable-cuda], [enable cuda [default=no]])],
  [zmq_enable_cuda=$enableval],
  [zmq_enable_cuda=no])

echo enable_cuda=$zmq_enable_cuda
AM_CONDITIONAL([ENABLE_CUDA], [false])
if test "x$zmq_enable_cuda" == "xyes" -o "x$zmq_enable_cuda" == "x"; then
  AC_DEFINE(QUICKPOOL_WITH_CUDA, 1, "Have CUDA integration with quickpool.")
  AM_CONDITIONAL([ENABLE_CUDA], [true])
fi

# Set some cluster configuration variables .
AC_ARG_VAR([quickpooladmin], [Username of the quickpool admin])
AC_ARG_VAR([quickpoolcluster], [Name of the cluster])
# Set some defaults.
: ${quickpooladmin:=quickpool}
: ${quickpoolcluster:=quickpool}
AC_SUBST([quickpooladmin])
AC_SUBST([quickpoolcluster])
AC_MSG_RESULT([username for quickpool admin ... ${quickpooladmin}])
AC_MSG_RESULT([name for quickpool cluster ... ${quickpoolcluster}])
LDFLAGS="$LDFLAGS -lm -lpthread -ldl -no-install"

AC_CONFIG_FILES([
    Makefile                 \
    common/Makefile	     \
    chkpnt/Makefile          \
    config/Makefile          \
    lib/Makefile             \
    api/Makefile             \
    jm/Makefile              \
    js/Makefile              \
    ee/Makefile              \
    policy/Makefile	     \
    cli/subcmd/Makefile      \
	cli/Makefile             \
	scripts/Makefile         \
	config/qp.conf          \
	config/qp.hosts         \
	config/qp.params        \
	config/qp.queues        \
	config/qp.users         \
	config/qp.cluster.${quickpoolcluster}:config/qp.cluster.quickpool.in \
	config/qp.shared        \
	config/quickpool.csh      \
	config/quickpool.sh],,
        [quickpoolcluster=$quickpoolcluster]
)

AC_OUTPUT
